from __future__ import division

import logging, unittest, time, tempfile, os

import numpy.testing, scipy.integrate, scipy.constants

import test_util
import kinetics_opt, kinetics_opt_main_test, kinetics_opt_plots

logger = logging.getLogger("kinetics_opt_main_test")
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)


class KineticsOptPlotsTest(unittest.TestCase):

    def test_shared_dx_mle(self):
        temp_path0 = kinetics_opt_main_test.make_cs0_file()
        temp_path1 = kinetics_opt_main_test.make_cs1_file()
        actual = kinetics_opt_plots.main([
                "--in_repr_paths", "", "",
                "--out_repr_paths", "", "",
                "--curve_stretch_playlists", temp_path0, temp_path1,
                "--dxs_nm", "0.16", "0.17", "0.20", "0.25",
                "--k0s", "0.01", "0.03", "0.04", "0.1",
                "--normal_dxs_nm", "0", "0", "0", "0",
                "--normal_k0s", "0", "0", "0", "0",
                "--truncated_normal_dxs_nm", "0", "0", "0", "0",
                "--truncated_normal_k0s", "0", "0", "0", "0",
                "--labels", "x", "a", "b", "y", "c", "d",
                "--diffuse_half_support_units", "3",
                ])
        os.remove(temp_path0)
        os.remove(temp_path1)
        self.assertTrue(actual)

    def test_batch_size_does_not_affect_results(self):
        temp_path0 = kinetics_opt_main_test.make_cs0_file()
        temp_path1 = kinetics_opt_main_test.make_cs1_file()
        actual = kinetics_opt_plots.main([
                "--in_repr_paths", "", "",
                "--out_repr_paths", "", "",
                "--curve_stretch_playlists", temp_path0, temp_path1,
                "--dxs_nm", "0.16", "0.17", "0.20", "0.25",
                "--k0s", "0.01", "0.03", "0.04", "0.1",
                "--normal_dxs_nm", "0.16", "0.17", "0.20", "0.25",#"0", "0", "0", "0",
                "--normal_k0s", "0.01", "0.03", "0.04", "0.1",#"0", "0", "0", "0",
                "--truncated_normal_dxs_nm", "0", "0", "0", "0",
                "--truncated_normal_k0s", "0", "0", "0", "0",
                "--batch_size", "1",
                "--labels", "x", "a", "b", "y", "c", "d",
                "--diffuse_half_support_units", "3",
                "--pn_per_unit", "10",
                "--upper_bound_units", "31",
                ])
        expected = kinetics_opt_plots.main([
                "--in_repr_paths", "", "",
                "--out_repr_paths", "", "",
                "--curve_stretch_playlists", temp_path0, temp_path1,
                "--dxs_nm", "0.16", "0.17", "0.20", "0.25",
                "--k0s", "0.01", "0.03", "0.04", "0.1",
                "--normal_dxs_nm", "0.16", "0.17", "0.20", "0.25",#"0", "0", "0", "0",
                "--normal_k0s", "0.01", "0.03", "0.04", "0.1",#"0", "0", "0", "0",
                "--truncated_normal_dxs_nm", "0", "0", "0", "0",
                "--truncated_normal_k0s", "0", "0", "0", "0",
                "--batch_size", "512",
                "--labels", "x", "a", "b", "y", "c", "d",
                "--diffuse_half_support_units", "3",
                "--pn_per_unit", "10",
                "--upper_bound_units", "31",
                ])
        os.remove(temp_path0)
        os.remove(temp_path1)
        self.assertTrue(actual)
        self.assertTrue(expected)
        test_util.assert_allclose_recursive(actual[0], expected[0], rtol=1e-7)

if __name__ == "__main__":
    suite = unittest.TestLoader().loadTestsFromTestCase(KineticsOptPlotsTest)
    unittest.TextTestRunner(verbosity=2).run(suite)
